$(function () {
    selectHeadLink();
})

function selectHeadLink() {
    $.ajax({
        url: '/back/mainPage/selectHeadLink',
        data: '',
        dataType: 'json',
        type: 'get',
        success: function (data) {
            let list = data.list;
            let html = '';
            for (let i = 0; i < list.length; i++) {
                html += '<tr>\n' +
                    '            <td>' + list[i].id + '</td>\n' +
                    '            <td>' + list[i].title + '</td>\n' +
                    '            <td>' + list[i].href + '</td>\n' +
                    '            <td><a href="javascript:add()">新增</a>&nbsp;' +
                    '                <a href="javascript:deleteHeadLink(' + list[i].id + ')">删除</a>&nbsp;' +
                    '                <a href="javascript:edit(' + list[i].id + ')">修改</a>' +
                    '            </td>\n' +
                    '        </tr>';
            }
            $("#findHeadLink").html(html);

        }
    });

}

//新增信息==================================
function add() {
    let html = '';
    html += '<div style="float: left;margin-right:10px">地址名称：<input id="title" type="text"></div>\n' +
        '    <div style="float: left;margin-right:10px">地址链接：<input id="href" type="text"></div>\n' +
        '    <div style="float: left;margin-right:10px"><button onclick="addHeadLink()">新增</button></div>';
    $("#editHeadLink").html(html);
}

function addHeadLink() {
    let data = {
        title: $("#title").val(),
        href: $("#href").val()
    };
    console.log(data);
    $.ajax({
        url: '/back/mainPage/addHeadLink',
        data: data,
        dataType: 'json',
        type: 'post',
        success: function (data) {
            if (data.results == 1) {
                alert("添加成功");
                $('#right').load('/back/html/mainPage/headLinkManage.html');
            } else {
                alert("添加失败");
            }
        }
    });
}

//修改信息==================================
function edit(id) {
    $.ajax({
        url: '/back/mainPage/selectHeadLinkById',
        data: {id: id},
        dataType: 'json',
        type: 'get',
        success: function (data) {
            data = data.model;
            let html = '';
            html += '<div>地址名称：<input id="title" type="text" value=' + data.title + '></div>\n' +
                '    <div>地址链接：<textarea id="href">' + data.href + '</textarea></div>\n' +
                '    <div><button onclick="updateHeadLink(' + data.id + ')">确定修改</button></div>';
            $("#editHeadLink").html(html);
        }
    })
}

//更新HeadandLink的信息
function updateHeadLink(id) {
    let data = {
        id:id,
        title: $("#title").val(),
        href: $("#href").val()
    };
    $.ajax({
        url: '/back/mainPage/updateHeadLinkById',
        data: data,
        dataType: 'json',
        type: 'post',
        success: function (data) {
            console.log(data)
            if (data.result == 1) {
                alert("更新成功");
                $('#right').load('/back/html/mainPage/headLinkManage.html');
            } else {
                alert("更新失败");
            }
        }
    })
}
function deleteHeadLink(id) {
    $.ajax({
        url: '/back/mainPage/deleteHeadLink',
        data: {id:id},
        dataType: 'json',
        type: 'post',
        success: function (data) {
            if (data.result == 1) {
                alert("更新成功");
                $('#right').load('/back/html/mainPage/headLinkManage.html');
            } else {
                alert("更新失败");
                $('#right').load('/back/html/mainPage/headLinkManage.html');
            }
        }
    })
}